package servlet;

import util.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author leiHua
 * @Date 2022/10/14 17:39
 */
@WebServlet("/doUpdateMajor.do")
public class UpdateMajorServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        Connection conn = null;
        PreparedStatement ps = null;
        String name = request.getParameter("name");
        String id = request.getParameter("id");
        try {
            conn=JDBCUtils.getconnection();
            String sql = "update tb_major set major_name=? where id=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, id);
            int i = ps.executeUpdate();
            if (i == 1) {
                request.getRequestDispatcher("/showAllMajors.do").forward(request,response);
            }
        }  catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                JDBCUtils.close(null,ps,conn);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
